<template>
  <div class="system-user-dialog-container">
    <el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="500px">
      <el-form ref="userDialogFormRef" :model="state.ruleForm" size="default" label-width="100px">
        <el-form-item label="任务类型名称">
          <el-input v-model="state.ruleForm.name" placeholder="请输入任务类型名称" clearable></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import {reactive, ref} from "vue";
import {addTaskType, editTaskType} from "/@/views/taskType/taskType";
// 定义子组件向父组件传值/事件
const emit = defineEmits(["refresh"]);


// 定义变量内容
const userDialogFormRef = ref();
const state = reactive({
  tags: [],
  users: [],
  ruleForm: {
    id: "",
    name: "",
    status: 1
  },
  dialog: {
    isShowDialog: false,
    type: "",
    title: "",
    submitTxt: ""
  }
});
// 打开弹窗
const openDialog = (type: string, row: any, tags: any, users: any) => {
  state.tags = tags;
  state.users = users;
  if (type === "edit") {
    state.ruleForm.id = row.id;
    state.ruleForm.name = row.name;
    state.ruleForm.status = row.status;

    state.dialog.title = "编辑任务类型";
    state.dialog.submitTxt = "保 存";
    state.dialog.type = "edit";
  } else {
    state.ruleForm = {
      id: "",
      name: "",
      status: 1
    },
        state.dialog.title = "新增任务类型";
    state.dialog.submitTxt = "新 增";
    state.dialog.type = "add";
  }
  state.dialog.isShowDialog = true;
};
// 关闭弹窗
const closeDialog = () => {
  state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
  closeDialog();
};
// 提交
const onSubmit = () => {
  closeDialog();
  if (state.dialog.type === 'edit') {
    editTaskType(state.ruleForm).then((res) => {
      emit('refresh');
    });
  } else {
    addTaskType(state.ruleForm).then(res => {
      emit("refresh");
    });
  }
};

// 暴露变量
defineExpose({
  openDialog
});
</script>
